package cn.wx.shop.web.controller;

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.wx.shop.entity.SysFile;
import cn.wx.shop.result.AjaxFileResult;
import cn.wx.shop.result.AjaxResult;
import cn.wx.shop.service.SysFileService;

/**
 * 文件通用控制器
 * 上传
 * 显示
 * 下载
 * 
 * @author acer
 *
 */
@Controller
public class FileController {

	private String filePath = "E:\\wxShopFile\\"; //文件存储地址
	
	@Autowired
	SysFileService sysFileService;

	@ResponseBody
	@RequestMapping("/keupload")
	public AjaxFileResult keupload(MultipartFile imgFile){
		return uploadFile(imgFile);
	}
	@ResponseBody
	@RequestMapping("/upload")
	public AjaxFileResult upload(MultipartFile file){
		
		return uploadFile(file);
	}
	private AjaxFileResult uploadFile(MultipartFile file) {
		try {
			if(!file.isEmpty()){
				
				//存物理
				//文件后缀
				String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
				String fileName = UUID.randomUUID().toString().replace("-", "");
				String saveFilePath = filePath + fileName + suffix;
				File saveFile = new File(saveFilePath);
				file.transferTo(saveFile);

				//数据库
				SysFile sysFile = new SysFile();
				sysFile.setFileId(fileName);
				sysFile.setFilePath(saveFilePath);
				sysFileService.addFile(sysFile);
				
				AjaxFileResult ajaxFileResult = new AjaxFileResult();
				ajaxFileResult.setFileId(fileName);
				ajaxFileResult.setStatus(true);
				ajaxFileResult.setCode(200);
				ajaxFileResult.setMessage("上传成功");
				//针对富文本编辑器
				ajaxFileResult.setUrl("/wxShop/view/"+fileName);
				ajaxFileResult.setError(0);

				return ajaxFileResult;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		AjaxFileResult ajaxFileResult = new AjaxFileResult();
		ajaxFileResult.setCode(500);
		ajaxFileResult.setMessage("上传失败");
		//针对富文本编辑器
		ajaxFileResult.setError(1);
		
		return ajaxFileResult;
	}
	
	@ResponseBody
	@RequestMapping("/view/{fileId}")
	public void getFile(@PathVariable String fileId,HttpServletResponse response){
		
		try {
			//查询数据库fileId对应的路径
			SysFile sysFile = sysFileService.getFile(fileId);
			
			File file = new File(sysFile.getFilePath());
			byte[] buffer = new byte[(int) file.length()];
			
			//读
			FileInputStream in = new FileInputStream(file);
			in.read(buffer);
			in.close();
			response.getOutputStream().write(buffer);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {

		String fileName = UUID.randomUUID().toString().replace("-", "");
		System.out.println(fileName);
	}
}
